package com.zhangwei.liwushuo.fragment;

import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.zhangwei.liwushuo.R;
import com.zhangwei.liwushuo.bean.AppEntity;
import com.zhangwei.liwushuo.bean.DanPinEntity;
import com.zhangwei.liwushuo.bean.Data;
import com.zhangwei.liwushuo.config.AppConstants;
import com.zhangwei.liwushuo.utils.BaseAsyncHttpClient;
import com.zhangwei.liwushuo.adapter.vh.DanPinItemView;
import com.zw.library.adapter.AAdapter;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 *
 */
@EFragment(R.layout.fragment_danpin)
public class DanPinFragment extends BaseFragment implements AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2<GridView> {
    @Bean
    BaseAsyncHttpClient client;
    @ViewById
    PullToRefreshGridView danpinPtrGv;
    private AAdapter<DanPinEntity> adapter;
    private int offset = 0;
    private TypeReference<AppEntity<Data<DanPinEntity>>> type;
    private String url;


    @Override
    public void beforeInject() {
        super.beforeInject();
        bus.register(this);
        url = AppConstants.getDanpinItemUrl(0);
        type = new TypeReference<AppEntity<Data<DanPinEntity>>>() {
        };
    }

    @Override
    public void initView() {
        danpinPtrGv.setOnRefreshListener(this);
        adapter = new AAdapter<DanPinEntity>(DanPinItemView.class, getActivity());
        danpinPtrGv.setOnItemClickListener(this);
        danpinPtrGv.setAdapter(adapter);
    }

    public void initData() {
        helper.get(url, adapter, danpinPtrGv, type, offset);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
        offset = 0;
        url = AppConstants.getDanpinItemUrl(0);
        initData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
        offset = 1;
        initData();
    }
//    @Subscribe
    public void nextUrl(String url) {
        this.url = url;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        bus.unregister(this);
    }
}
